package cn.coastalinfo.project.drools.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import cn.coastalinfo.project.drools.domain.vo.RuleConditionVo;
import cn.coastalinfo.project.drools.domain.bo.RuleConditionBo;
import cn.coastalinfo.project.drools.service.IRuleConditionService;
import cn.coastalinfo.framework.web.controller.BaseController;
import cn.coastalinfo.framework.web.domain.AjaxResult;
import cn.coastalinfo.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;


import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 规则条件Controller
 * 
 * @author hadge
 * @date 2024-06-08
 */
@Api("规则内容编辑")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drools/condition")
public class RuleConditionController extends BaseController
{
    @Autowired
    private IRuleConditionService ruleConditionService;

    /**
     * 查询规则条件列表
     */
    //@PreAuthorize("@ss.hasPermi('drools:condition:list')")
    @GetMapping("/list")
    public AjaxResult list(RuleConditionBo ruleConditionBo)
    {
        List<RuleConditionVo> list = ruleConditionService.queryList(ruleConditionBo);
        return success(list);
    }

    /**
     * 导出规则条件列表
     */
    //@PreAuthorize("@ss.hasPermi('drools:condition:export')")
    //@Log(title = "规则条件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RuleConditionBo ruleConditionBo)
    {
        List<RuleConditionVo> list = ruleConditionService.queryList(ruleConditionBo);
        ExcelUtil<RuleConditionVo> util = new ExcelUtil<RuleConditionVo>(RuleConditionVo.class);
        util.exportExcel(response, list, "规则条件数据");
    }

    /**
     * 获取规则条件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('drools:condition:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id)
    {
        return success(ruleConditionService.queryById(id));
    }

    /**
     * 新增规则条件
     */
    //@PreAuthorize("@ss.hasPermi('drools:condition:add')")
    //@Log(title = "规则条件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RuleConditionBo ruleConditionBo)
    {
        return toAjax(ruleConditionService.insertByBo(ruleConditionBo));
    }

    /**
     * 修改规则条件
     */
    //@PreAuthorize("@ss.hasPermi('drools:condition:edit')")
    //@Log(title = "规则条件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RuleConditionBo ruleConditionBo)
    {
        return toAjax(ruleConditionService.updateByBo(ruleConditionBo));
    }

    /**
     * 删除规则条件
     */
    //@PreAuthorize("@ss.hasPermi('drools:condition:remove')")
    //@Log(title = "规则条件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids)
    {
        return toAjax(ruleConditionService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("更新规则内容")
    //@Log(title = "更新规则", businessType = BusinessType.UPDATE)
    @PostMapping("/saveTree")
    @Transactional
    public AjaxResult saveTree(@RequestBody RuleConditionBo ruleConditionBo)
    {
        return toAjax(ruleConditionService.saveTree(ruleConditionBo));
    }

    @ApiOperation("获取规则内容")
    @GetMapping(value = "/info/{ruleId}")
    public AjaxResult getTreeInfo(@NotNull(message = "规则主键不能为空") @PathVariable Long ruleId)
    {
        return success(ruleConditionService.treeInfoByRuleId(ruleId));
    }

    @ApiOperation("获取规则所有参数")
    @GetMapping(value = "/getParams")
    public AjaxResult getParams(@PathVariable String groupName)
    {
        return success(ruleConditionService.getParams(groupName));
    }
}
